using System.Collections.Generic;
using System.Web.Mvc;
using Animoso.Domain;
using Animoso.Domain.Data;

namespace Animoso.Web.Controllers
{
    public class SectionController : ApplicationController
    {
        private readonly IRepository<Section> _sectionRepository;
        private readonly IRepository<Page> _pageRepository;

        public SectionController(IRepository<Section> repository, IRepository<Page> pageRepository)
        {
            _sectionRepository = repository;
            _pageRepository = pageRepository;
        }

        public JsonResult Add(string title, Position position)
        {
            Section section = new Section()
                                  {
                                      Title = title,
                                      Position = position
                                  };
            _sectionRepository.Add(section);

            return Json(section, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            IEnumerable<Section> sections = _sectionRepository.GetAll();

            return View(sections);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult AddPage(int id)
        {
            ViewData["sectionId"] = id;
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddPage([Bind(Exclude = "Id")] Page content, int sectionId)
        {
            if(!ModelState.IsValid)
            {
                ViewData["sectionId"] = sectionId;
                return View(content);
            }

            Section section = _sectionRepository.GetById(sectionId);
            section.Pages.Add(content);
            _sectionRepository.Save(section);
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EditPage(int id)
        {
            Page page = _pageRepository.GetById(id);
            return View(page);
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditPage(Page page)
        {
            _pageRepository.Save(page);
            return RedirectToAction("Index");
        }

        public JsonResult DeletePage(int pageId)
        {
            Page page = _pageRepository.GetById(pageId);
            _pageRepository.Delete(page);

            return Json(true, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DeleteSection(int sectionId)
        {
            Section section = _sectionRepository.GetById(sectionId);
            _sectionRepository.Delete(section);

            return Json(true, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditSection(Section section)
        {
            if (!ModelState.IsValid)
                return View(section);
            
            _sectionRepository.Save(section);
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EditSection(int id)
        {
            Section section = _sectionRepository.GetById(id);
            return View(section);
        }
    }
}